/**
 * 配置相关API服务
 */

import api, { getAuthHeaders } from './api';

export interface UserConfig {
  case_sensitive: boolean;
  sound_enabled: boolean;
  duration: number;
  sequence_length: number;
}

export class ConfigService {
  /**
   * 获取用户配置
   */
  static async getConfig(): Promise<UserConfig> {
    const response = await fetch(`${api.baseURL}/config`, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
        ...getAuthHeaders(),
      },
    });

    if (!response.ok) {
      throw new Error('获取配置失败');
    }

    return response.json();
  }

  /**
   * 更新用户配置
   */
  static async updateConfig(data: Partial<UserConfig>): Promise<UserConfig> {
    const response = await fetch(`${api.baseURL}/config`, {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json',
        ...getAuthHeaders(),
      },
      body: JSON.stringify(data),
    });

    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.message || '更新配置失败');
    }

    const result = await response.json();
    return result.config;
  }
}
